<template>
	<view class="password pages">
		<view class="form">
			<view class="input_container">
				<view class="input_title">
					{{$t('option.security.setPassword.Code')}}
				</view>
			    <view class="input">
					<input type="text" :placeholder="$t('option.security.setPassword.inCode')" placeholder-class="inner_class" v-model="code" >
				</view>
				<view class="input_btn" v-if="countdownShow">{{$t('option.security.setPassword.Retrieve')}}({{num}})</view>
				<view class="input_btn" v-else @click="getCode">{{$t('option.security.setPassword.obCode')}}</view>
			</view>
			<view class="input_container">
				<view class="input_title">
					{{$t('option.security.setPassword.newPassword')}}
				</view>
				<view class="input">
					<input type="text" :placeholder="$t('option.security.setPassword.newPass')" placeholder-class="inner_class" v-model="newPassword"
						:password="showNewPassword">
					<uni-icons type="eye" color="#b7b7c4" size="20" @tap="showNewPassword=!showNewPassword"></uni-icons>
				</view>
			</view>
			<!-- 提示 -->
			<view class="hint">
				{{$t('option.security.setPassword.Must')}}
			</view>
			<view class="save_btn" @tap="submit" :class="code!=''&&newPassword!=''?'active':''">
				{{$t('option.security.setPassword.Complete')}}
			</view>
		</view>
	</view>
</template>

<script>
	import { sendSms, userUpdatePass, userUpdateSecuri ,smsAPI} from '@/api/api.js'
	export default {
		data() {
			return {
				event: "",
				key: "",
				showNewPassword: true,
				code: "",
				newPassword: "",
				countdownShow: false,
				num: 60,
			}
		},
		onLoad(opt) {
			this.key = opt.key
			if (opt.key == "login") {
				this.event = 'pass'
				uni.setNavigationBarTitle({
					title: this.$t('option.security.setPassword.Set')
				})
			} else {
				this.event = 'paypass'
				uni.setNavigationBarTitle({
					title: this.$t('option.security.setPassword.setPass')
				})
			}
		},
		methods: {
			submit() {
				switch (this.key) {
					case "login":
						this.userUpdatePass()
						break;
					case "pay":
						this.userUpdateSecuri()
						break
					default:
						break;
				}
			},
			// 倒计时
			getNum() {
				let time = setInterval(() => {
					this.num--
					if (this.num == -1) {
						clearTimeout(time);
						this.countdownShow = false
						this.num = 60
					}
				}, 1000)
			},
			getCode(){
				smsAPI({
					phone: uni.getStorageSync('userinfo').phone,
					phone_ex: uni.getStorageSync('userinfo').phone_ex,
					event: this.event
				}).then(res => {
					uni.showToast({
						title: this.$t('option.security.setPassword.aSend'),
						icon: "none"
					})
					this.code = res.data.data.code
					this.countdownShow = true
					this.getNum()
				})
			},
			// 登录密码
			userUpdatePass(){
				userUpdatePass({
					token: uni.getStorageSync('token'),
					code: this.code,
					new_pass: this.newPassword
				}).then(res=>{
					uni.showToast({
						title: res.data.msg,
						icon: "none"
					})
					setTimeout(() => {
						uni.clearStorageSync();
						uni.reLaunch({
							url: "/pages/shop/login/other"
						})
					}, 600)
				})
			},
			// 安全密码
			userUpdateSecuri(){
				userUpdateSecuri({
					token: uni.getStorageSync('token'),
					code: this.code,
					new_pass: this.newPassword
				}).then(res=>{
					uni.showToast({
						title: res.data.msg,
						icon: "none"
					})
					setTimeout(() => {
						uni.redirectTo({
							url: "/pages/shop/mine/mine"
						})
					}, 600)
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.password {
		width: 100%;

		.form {
			padding: 0 40rpx;

			.input_container {
				display: flex;
				align-items: center;
				width: 100%;
				height: 110rpx;
				border-bottom: 2rpx solid #ebebf0;
				font-size: 28rpx;
				color: #1b2c3d;

				.input_title {
					min-width: 200rpx;
				}

				.input {
					flex: 1;
					height: 100%;
					display: flex;
					align-items: center;

					.inner_class {
						font-size: 28rpx;
						color: #ccc;
					}
				}
				
				.input_btn{
					height: 70rpx;
					line-height: 70rpx;
					background-color: #00CBCC;
					color: #fff;
					padding: 0 20rpx;
				}
			}

			.hint {
				margin-top: 20rpx;
				font-size: 24rpx;
				line-height: 28rpx;
				color: #aaaabb;
			}

			.save_btn {
				width: 100%;
				height: 90rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				background: #aaaabb;
				color: #fff;
				margin-top: 40rpx;
			}

			.active {
				background: #00cbcc;
			}
		}
	}
</style>